package com.qiwenshare.file.controller;

import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.file.domain.UserBean;
import com.qiwenshare.file.dto.file.CreateFileDTO;
import com.qiwenshare.file.util.FileUtils;
import com.qiwenshare.file.util.JsonUtils;
import com.qiwenshare.file.vo.device.DeviceInfo;
import com.qiwenshare.file.vo.device.WSDataType;
import com.qiwenshare.file.websocket.WebSocketServer;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.Session;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Tag(name = "socketserver", description = "该接口用来管理设备")
@RestController
@Slf4j
@RequestMapping("/socketserver")
public class SocketServerController {
    @Resource
    WebSocketServer webSocketServer;

    public static final String CURRENT_MODULE = "设备管理";
    @Operation(summary = "获取设备", description = "获取设备", tags = {"listdevice"})
    @GetMapping(value = "/listdevice")
    @MyLog(operation = "获取设备", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> listdevice() {
        List<DeviceInfo> deviceinfos = new ArrayList<DeviceInfo>();
        for(Map.Entry<String, Session> entry: webSocketServer.sessionPools.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            deviceinfos.add(webSocketServer.devicePools.get(entry.getValue()));
        }
        return RestResult.success().data(deviceinfos);
    }

    @Operation(summary = "设置音量", description = "设置音量", tags = {"setvolumn"})
    @PostMapping(value = "/setvolumn")
    @MyLog(operation = "设置音量", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> setvolumn(@RequestBody DeviceInfo deviceInfoDto) {
        WSDataType wsDataType = new WSDataType();
        wsDataType.setMsgType("setvolumn");
        wsDataType.setData(deviceInfoDto.getVolumn().toString());
        webSocketServer.sendInfo(deviceInfoDto.getIp(), JsonUtils.objectToJson(wsDataType));
        Session session = webSocketServer.sessionPools.get(deviceInfoDto.getIp());
        webSocketServer.devicePools.get(session).setVolumn(deviceInfoDto.getVolumn());
        return RestResult.success().data(true);
    }

    @Operation(summary = "重启设备", description = "重启设备", tags = {"reboot"})
    @PostMapping(value = "/reboot")
    @MyLog(operation = "重启设备", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> reboot(@RequestBody DeviceInfo deviceInfoDto) {
        WSDataType wsDataType = new WSDataType();
        wsDataType.setMsgType("reboot");
        wsDataType.setData("");
        webSocketServer.sendInfo(deviceInfoDto.getIp(), JsonUtils.objectToJson(wsDataType));
        return RestResult.success().data(true);
    }

    @Operation(summary = "推送数据", description = "推送数据", tags = {"pushdata"})
    @PostMapping(value = "/pushdata")
    @MyLog(operation = "重启设备", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> pushdata(@RequestBody DeviceInfo deviceInfoDto) {
        WSDataType wsDataType = new WSDataType();
        wsDataType.setMsgType("polldata");
        wsDataType.setData("");
        webSocketServer.sendInfo(deviceInfoDto.getIp(), JsonUtils.objectToJson(wsDataType));
        return RestResult.success().data(true);
    }
}
